﻿using UFIDA.U9.MO.Complete;
using UFIDA.U9.SM.Ship;
using UFSoft.UBF.Business;
using UFSoft.UBF.Eventing;
using UFIDA.U9.MO.Enums;
using UFIDA.U9.MO.MO;
using System.Collections.Generic;
using UFSoft.UBF.Security;
using System.Reflection.Emit;
using UFIDA.U9.SM.DealerSO;
using System;
using UFSoft.UBF.Util.DataAccess;
using UFIDA.U9.PM.Rcv;

namespace UFIDA.U9.Cust.LS.PlugBE
{
    /// <summary>
    /// 鑫达
    /// 收货
    /// 1. 收货单行启用公共段1长、公共段2宽，批次主档启用私有段1长、2宽
    /// 2.当收货单审核时，把行公共段1长、公共段2宽，下入对应的批次档案私有1长、2宽，无批次的跳过

    /// </summary>
    internal class XWshdExtend : IEventSubscriber
    {
        //配置XX.sub.xml文件放置到portal/bin下，dll文件放置到portal/ApplicationServer/Libs下
        public void Notify(params object[] args)
        {
            if (args != null && args.Length != 0 && args[0] is EntityEvent)
            {
                BusinessEntity.EntityKey key = ((EntityEvent)args[0]).EntityKey;
                 Receivement receivement = key.GetEntity() as Receivement; // 收货单 
                if (receivement == null)
                    return;
               if (receivement.Status == RcvStatusEnum.InStoreConfirmed &&  receivement.OriginalData.Status == UFIDA.U9.PM.Rcv.RcvStatusEnum.Approving) //之前的状态是审核中
                 {
                    foreach (var rcvLines in receivement.RcvLines)
                    {
                        UFIDA.U9.Lot.LotMaster lot = UFIDA.U9.Lot.LotMaster.Finder.Find($"LotCode='{rcvLines.InvLotCode}'");
                        if (lot == null) return;
                        using (ISession session =  Session.Open())
                        { 
                            lot.DescFlexSegments.PrivateDescSeg1 = rcvLines.DescFlexSegments.PubDescSeg1;
                            lot.DescFlexSegments.PrivateDescSeg2 = rcvLines.DescFlexSegments.PubDescSeg2;
                            lot.DescFlexSegments.PrivateDescSeg3 = rcvLines.DescFlexSegments.PubDescSeg9;
                            lot.DescFlexSegments.PrivateDescSeg4 = rcvLines.DescFlexSegments.PubDescSeg10;
                            lot.DescFlexSegments.PrivateDescSeg5 = rcvLines.DescFlexSegments.PubDescSeg13;
                            session.Commit();
                        }

                    }

                }


            }
        } 

    }
}
